﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data;

namespace QN_DiemSV.Forms
{
    public partial class frmMH : Form
    {
        public frmMH()
        {
            InitializeComponent();
        }
        bool them = false;
        bool sua = false;
        Classes.connect conn = new QN_DiemSV.Classes.connect();
        void VisionControl()
        {
            txtMaMon.Enabled = true;
            txtTenMon.Enabled = true;
            txtSDVHT.Enabled = true;
            cboKhoa.Enabled = true;
        }
        void LoadToCombobox()
        {
            DataTable dt = conn.excuteSelect("select MaKhoa from KHOA");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cboKhoa.Items.Add(dt.Rows[i][0].ToString());
            }
        }
        void LoadData()
        {
            DataTable dt = new DataTable();
            dt = conn.excuteSelect("select * from MONHOC");
            dgrMON.DataSource = dt;
        }
        private void frmMH_Load(object sender, EventArgs e)
        {
            txtMaMon.Enabled = false;
            txtTenMon.Enabled = false;
            txtSDVHT.Enabled = false;
            cboKhoa.Enabled = false;
            LoadData();
            DataTable dt = conn.excuteSelect("select MaKhoa from KHOA");
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            them = true;
            VisionControl();
            txtMaMon.Focus();
            LoadToCombobox();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (them == true)
            {
                conn.EXE_PROC_PARAMS("MonHocInsert", "@MaMH", txtMaMon.Text, "@TenMH", txtTenMon.Text, "@SoTinChi", Convert.ToInt32(txtSDVHT.Text), "@MaKhoa", cboKhoa.SelectedItem.ToString());
                LoadData();
            }
            if (sua == true)
            {
                conn.excuteNonquery("update MONHOC set TenMH ='" + txtTenMon.Text + "', SoTinChi=" + Int32.Parse(txtSDVHT.Text) + ", MaKhoa='"+cboKhoa.Text+"' where MaMH='" + txtMaMon.Text.Trim() + "'");
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            sua = true;
            VisionControl();
        }

        private void dgrMON_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadToCombobox();
            int index = e.RowIndex;
            txtMaMon.Text = dgrMON.Rows[index].Cells["MaMH"].Value.ToString();
            txtSDVHT.Text = dgrMON.Rows[index].Cells["SoTinChi"].Value.ToString();
            txtTenMon.Text = dgrMON.Rows[index].Cells["TenMH"].Value.ToString();
            cboKhoa.Text = dgrMON.Rows[index].Cells["MaKhoa"].Value.ToString();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            conn.excuteNonquery("delete from MONHOC where MaMH='" + txtMaMon.Text + "'");
            LoadData();
        }
    }
}
